require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsHeroInteractionPointHandler ={};
local this = RedDotsHeroInteractionPointHandler;
function RedDotsHeroInteractionPointHandler.init()
    CloseEvent(Config.EventType.Fresh_Active,this.update);
    ListenEvent(Config.EventType.Fresh_Active,this.update);
    CloseEvent(Config.EventType.Fresh_HeroInteraction,this.update);
    ListenEvent(Config.EventType.Fresh_HeroInteraction,this.update);
    return this;
end

--参数1：指定英雄Id，nil代表检查所有英雄
---英雄详情突破页签红点
function RedDotsHeroInteractionPointHandler.getFlag(partnerId)
    local heroData=logicMgr.HeroManager.getHeroDatabyPartnerId(partnerId,true)
    if table.isNullOrEmpty(heroData)then
        return Config.RedDotsFlagState.None
    end
    if heroData.favorLv == logicMgr.HeroFavorManager.GetMaxFavorLevel() then
        return Config.RedDotsFlagState.None
    end
    local interactInfo=logicMgr.HeroFavorManager.getInteractionInfo()
    local num=interactInfo[1].f_FrequencyCo-logicMgr.HeroManager.getHeroInteractionNum()
    local function GetInteractionData(interactId)
        local interactData={}
        for i,v in pairs(heroData.interactInfos or{})do
            if v.id==interactId then
                interactData=v
                break
            end
        end
        return interactData
    end

    if num>0 then
        for i,v in ipairs(interactInfo)do
            local data=v
            if data.f_FrequencyType==1 then
                local interactData=GetInteractionData(data.f_InteractionId) or {}
                if table.isNullOrEmpty(interactData)then
                    interactData.times=0
                end
                if interactData.times<data.f_Frequency then
                    return Config.RedDotsFlagState.Needful
                end
            end
        end
    end
    return Config.RedDotsFlagState.None;
end

function RedDotsHeroInteractionPointHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.HeroInteractionPoint);
end